package com.wf.smartcomm.server.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Image;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.service.ImageService;
import com.wf.smartcomm.server.util.IdUtils;


@Service
@Transactional
public class ImageServiceImpl implements ImageService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Image image) {
		image.setObjectId(IdUtils.uuid());
		factory.getCurrentSession().persist(image);
	}

	@Override
	public void save(List<Image> images) {
		for (Image image : images) {
			image.setObjectId(IdUtils.uuid());
			factory.getCurrentSession().persist(image);
		}
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Image getAvatar(User user) {
		Query query = factory.getCurrentSession().createQuery("from Image image where image.user = :user");
		query.setParameter("user", user);
		return (Image) query.uniqueResult();
	}

	@Override
	public void update(Image image) {
		factory.getCurrentSession().update(image);
	}
}
